<?php
/**
 * 《国家管理》实现类
 *
 * @author 孙晓晔
 * @version $Id: Country.class.php 1035 2010-03-26 05:24:59Z sunxy $
 */
require_once(dirname(__FILE__) . '/InfoUtil.class.php');

class Country extends InfoUtil {

	public $tbl = Constant::tbl_country;

	public $orderby = array(
		'-' => '请选择',
		'zone' => '区号',
		'rate' => '费率',
		'continent' => '洲',
	);

	function __construct() {
		$request['update'] = '$Date: 2009/02/16 07:36:07 $';
		$request['revision'] = '$Revision: 1.1 $';

		parent::__construct($request);
	}
	
	function tab($controller, $request) {		
		$data = array(
			
			'文件信息' => array('cc'=>'info.php?do=Filename.find', 'js'=>''),			
		);
		
		$ret = $this->_tab($data);

		$controller->assign_by_ref('result', $ret);
		
		$controller->display($request, 'tab');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function find($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/country/find');
		return true;
	}

	function _where($request) {
		$name_s = $request['name_s'];

		$zone_s = $request['zone_s'];
		$continent_s = $request['continent_s'];

		$where = " WHERE 1=1";

		if (!empty($name_s)) {
			$where .= " AND (name LIKE '%" . $name_s . "%' OR country LIKE '%" . $name_s . "%')";
		}

		if (!empty($zone_s)) {
			$where .= " AND zone = '" . $zone_s . "'";
		}

		if (!empty($continent_s) && $continent_s <> '-') {
			$where .= " AND continent = '" . $continent_s . "'";
		}

		$where .= $this->_where_default($request);
		$where = $this->_where_aa($where);

		return $where;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function findPost($controller, $request) {
		$this->pretreat($controller);

		$this->findPara($controller, $request);

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s a %s %s";
		$where = $this->_where($request);
		$orderby = $this->_orderby();
		$sql = sprintf($fmt, $this->tbl, $where, $orderby);
		debug($sql);

		$pager_option = array (
            'db' => $db,
            'sql' => $sql,
            'PageSize' => $this->pagesize,
            'CurrentPageID' => $request['p'],
            'numItems' => $request['n']
		);

		$pager = @new Pager($pager_option);
		$data = $pager->getPageData();

		if ($pager->number > 0) {

			$count = 0;
			$from = $pager->from;
			while ($count < $pager->number) {
				$row = $data[$count];
				$row = $this->_view(null, $row);

				$data[$count] = $row;
				$count++;
			}

			$controller->assign_by_ref('result', $data);
			$controller->assign_by_ref('pager', $pager);
		}

		$controller->display($request, 'info/country/list');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function add($controller, $request) {
		$this->pretreat($controller);

		$controller->display($request, 'info/country/add');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function addPost($controller, $request) {
		$this->pretreat($controller);

		$code = $request['code'];
		$zone = $request['zone'];
		$rate = $request['rate'];

		$name = $request['name'];
		$continent = $request['continent'];
		$country = addslashes($request['country']);

		$orderby = $request['orderby'];

		$status = $request['status'];

		clean($this->tbl . "_status_" . $status);

		$db = newdb($this->dsn_w);
		$fmt = "INSERT INTO %s(code, zone, rate, name, continent, country, orderby, status, createtime, modifytime)";
		$fmt .= " VALUES('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', now(), now())";
		$sql = sprintf($fmt, $this->tbl, $code, $zone, $rate, $name, $continent, $country, $orderby, $status);
		// debug($sql);
		$db->query($sql);
		
		$request['id_s'] = $db->insert_id();

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function edit($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/country/edit');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function editPost($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		clean($this->tbl . "_id_" . $id);

		$code = $request['code'];
		$zone = $request['zone'];
		$rate = $request['rate'];

		$name = $request['name'];
		$continent = $request['continent'];
		$country = addslashes($request['country']);

		$orderby = $request['orderby'];

		$status = $request['status'];

		clean($this->tbl . "_status_" . $status);

		$db = newdb($this->dsn);
		$fmt = "UPDATE %s SET code = '%s', zone = '%s', rate = '%s', name = '%s', continent = '%s', country = '%s'";
		$fmt .= ", orderby = '%s', status = '%s', modifytime = now() WHERE id = %s";
		$sql = sprintf($fmt, $this->tbl, $code, $zone, $rate, $name, $continent, $country, $orderby, $status, $id);
		// debug($sql);
		$db->query($sql);
		if ($db->error()) {
			$controller->assign("msg", "修改失败，请联系管理员" . $db->error());
			$controller->display($request, 'info');
			return false;
		}

		$controller->assign('baseLink', $this->doGet($request, 'findPost'));

		$this->findPost($controller, $request);
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function view($controller, $request) {
		$this->pretreat($controller);

		$id = $request['id'];
		$row = $this->_view($id);

		$controller->assign_by_ref('result', $row);

		$controller->display($request, 'info/country/view');
		return true;
	}
	
	function _view($id = 0, $row = null) {
		if ($row == null) {
			clean($this->tbl . "_id_" . $id);
			$row = $this->id($this->tbl, $id);
		}
		
		$row['continent_t'] = $this->continent[$row['continent']];
		
		$row = $this->_view_t($row);

		return $row;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function continent($controller, $request) {
		return $this->ajax_continent($controller, $request);
	}

	/**
	 *
	 *
	 * @param Controler $controller
	 * @param array $request
	 */
	function rate($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn_r);
		$fmt = "select zone, rate from %s order by zone";
		$sql = sprintf($fmt, $this->tbl);
		// debug($sql);
		$result = $db->query($sql);
		$data = array();
		$dept = '0';
		while ($row = $db->fetch_array($result)) {
			$data[] = $row;
		}

		$controller->assign_by_ref('result', $data);

		$request['tpl'] = 'txt';

		$controller->display($request, 'country/rate');
		return true;
	}

	/**
	 * 测试
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function a($controller, $request) {

		$db = newdb($this->dsn);
		$fd = fopen("/home/ftp/tecmold.com/ns/tecmold/doc/country.txt", "r");
		while (!feof($fd)) {
			$s = trim(fgets($fd));
			if (strlen($s) < 5) {
				continue;
			}

			$a = preg_split("/[\\s,]+/", trim($s));
			// print_r($a);
			$eng = $a[0];
			$name = $a[1];

			$country = '';
			for ($i = 2; $i < count($a); $i++) {
				$country .= " " . $a[$i];
			}

			$fmt = "insert into admin.country(eng, name, country, createtime, modifytime)";
			$fmt .= " VALUES('%s', '%s', '%s', now(), now())";
			$sql = sprintf($fmt, $eng, $name, $country);
			// debug($sql);
			$db->query($sql);
		}

		$controller->display($request, 't');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_continent($controller, $request) {
		if (!empty($request['continent'])) {
			$continent = $request['continent'];
		}
		if (!empty($request['continent_s'])) {
			$continent = $request['continent_s'];
		}

		if (empty($continent) || $continent == '-') {
			$data[] = array('code'=>'-', 'name'=>'请选择');
			echo json_encode($data);
			return false;
		}

		$key = "continent_" . $continent;
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				$controller->assign('result', json_encode($ret));

				$request['tpl'] = 'json';
				$controller->display($request, 'json');
				return true;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "SELECT * FROM %s WHERE continent = '%s' order by orderby";
		$sql = sprintf($fmt, Constant::tbl_country, $continent);
		// debug($sql);
		$result = $db->query($sql);
		$ret[] = array('code'=>'-', 'name'=>'请选择');
		$ret = array_merge($ret, $db->fetch_all($result));
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("3 day"));
		}

		$controller->assign('result', json_encode($ret));

		$request['tpl'] = 'json';
		$controller->display($request, 'json');
		return true;
	}

	/**
	 *
	 *
	 * @param Controller $controller
	 * @param array $request
	 */
	function ajax_zjm($controller, $request) {
		$this->pretreat($controller);
				
		$data = $this->_ajax_zjm($request);
		echo json_encode($data);
	}

	function _ajax_zjm($request) {
		$q = $request['q'];
		
		$key = sprintf("%s_%s_%s", $this->zjm, $this->prefix, $q);
		// debug($key);
		$mc = memcache("root");
		if (is_object($mc)) {
			$ret = $mc->get($key);
			if (is_array($ret)) {
				return $ret;
			}
		}

		$db = newdb($this->dsn_r);
		$fmt = "select id, code, zone, rate, name, zjm, continent, country from %s where zjm like '%s'";
		$sql = sprintf($fmt, $this->tbl, $q . "%");
		// debug($sql);
		$result = $db->query($sql);
		$ret = array();
		while ($row = $db->fetch_array($result)) {
			$row['continent_t'] = $this->continent[$row['continent']];

			$ret[] = $row;
		}
		
		if (is_object($mc) && count($ret) > 0) {
			$mc->set($key, $ret, 0, _strtotime("3 day"));
		}

		return $ret;
	}

	function z($controller, $request) {
		$this->pretreat($controller);

		$db = newdb($this->dsn);
		$fmt = "SELECT * FROM %s";
		$sql = sprintf($fmt, $this->tbl);
		$result = $db->query($sql);
		while ($row = $db->fetch_array($result)) {
			$zjm = getPinyin($row['name']);
			$zjm = strtoupper($zjm);

			$fmt = "UPDATE %s SET zjm = '%s' WHERE id = '%s'";
			$sql = sprintf($fmt, $this->tbl, $zjm, $row['id']);
			// debug($sql);
			$db->query($sql);
		}

		logdate($controller, $request);
		return true;
	}
	
	function t($controller, $request) {
		
		$v = array('cc'=>'info.php?do=Code.find', 'js'=>'');
		$b = "info.php?do=Code";
		$b = quotemeta($b);
		echo $b;
		if (preg_match("/$b/", $v['cc'])) {
			echo "OK";
		} else {
			echo "2222";
		}

		logdate($controller, $request);
		return true;
	}
}
?>